<?php 
/* 	These code was generated using phpCIGen v 2.a (21/05/2012)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #CV. Trust Solution, jl. Saronojiwo 2/19 Surabaya, http://www.ts.co.id

	+ Module  		: t_rapk Model
	+ Description	: For record model process back-end
	+ Filename 		: c_t_rapk.php
 	+ creator 		: 
 	+ Created on 24/Jul/2012 10:52:52

*/

class M_t_rapk extends CI_Model{

		//constructor
		function __construct() {
			parent::__construct();
		}
		
		function tes(){
		
			$unlock_tbl = "UNLOCK TABLES"; // edited by unu	
			$this->db->query($unlock_tbl);
		}
		
		function t_rapk_edit_subkon($rapk_id,$by){
			// get group atau parent //
			$sql = "SELECT * FROM t_rapkdet where t_rapk_id = ".$rapk_id." AND rapkdet_rgt <> rapkdet_lft + 1 ";
			$query = $this->db->query($sql);
			foreach($query->result() as $rows){
				$data = array(
					'group_uraian' => $rows->rapkdet_uraian,
					'group_publish' => 1,
					'rapkdet_id' => $rows->rapkdet_id,
					'group_created_by' => $by
				);
				$this->db->insert('t_rapkdet_grouptemp',$data);
				
				if($this->db->affected_rows()){
					$group_id = $this->db->insert_id();
					// get detail atau child //
					$sql = "SELECT * FROM t_rapkdet where t_rapk_id = ".$rapk_id." AND rapkdet_lft > ".$rows->rapkdet_lft.
							" AND rapkdet_rgt < ".$rows->rapkdet_rgt;
					$query = $this->db->query($sql);
					foreach($query->result() as $row){
						$data = array(
							'group_id' => $group_id,
							'rapkdet_id' => $row->rapkdet_id,
							'm_jobdet_id' => $row->m_jobdet_id,
							'rapkdet_volume' =>  $row->rapkdet_volume,
							'rapkdet_unit' => $row->rapkdet_unit,
							'rapkdet_price' => $row->rapkdet_price,
							'rapkdet_publish' =>  $row->rapkdet_publish
						);
						
						$this->db->insert('t_rapkdet_groupdettemp',$data);
					}
				}
			}
			return 1;
		}
		
		function rapkdet_list($task,$master_id,$query,$start,$end) {
			/*if($task=='detail'){
				$query = "SELECT rapkdet_id, jobdet_id, jobdet_name, rapkdet_volume, rapkdet_price, rapkdet_publish, unit_id, unit_name
						FROM t_rapkdet JOIN m_jobdet ON(t_rapkdet.m_jobdet_id = m_jobdet.jobdet_id)
							LEFT JOIN m_unit ON(m_unit.unit_id = t_rapkdet.rapkdet_unit)
						WHERE t_rapk_id=".$master_id;
			}elseif ($task=='byjob'){
				$query = "SELECT 0 AS rapkdet_id, jobdet_id, jobdet_name, 0 AS rapkdet_volume, 0 AS rapkdet_price, 0 AS rapkdet_publish, '' AS unit_id, '' AS unit_name
						FROM m_jobdet WHERE m_job_id=".$master_id;
			}*/
			
			if($task=='detail'){
				$sql = "SELECT div_name
						FROM t_rapk JOIN m_job ON(m_job.job_id = t_rapk.m_job_id)
						LEFT JOIN m_div ON  m_job.m_div_id = div_id
						WHERE t_rapk.rapk_id = ".$master_id;
				$rs = $this->db->query($sql);
				$m_div_id = $rs->row()->div_name;
				
				if($m_div_id == '503. Sub Kontraktor'){
					$sql = "SELECT t_rapkdet.rapkdet_id
							FROM t_rapkdet 
							WHERE t_rapkdet.t_rapk_id = ".$master_id." AND t_rapkdet.m_jobdet_id = 0 LIMIT 1";
					$rs = $this->db->query($sql);
					$t_rapkdet_group = $rs->row()->rapkdet_id;
					
					$query = "SELECT node.rapkdet_id, m_jobdet.jobdet_id, m_jobdet.jobdet_name, node.rapkdet_volume, node.rapkdet_price, node.rapkdet_publish, node.rapkdet_lft, node.rapkdet_rgt,
								m_unit.unit_id, m_unit.unit_name,
								(COUNT(parent.rapkdet_id) - (sub_tree.depth + 1)) AS depth
							FROM t_rapkdet AS node JOIN t_rapkdet AS parent ON(node.rapkdet_lft BETWEEN parent.rapkdet_lft AND parent.rapkdet_rgt)
								JOIN t_rapkdet AS sub_parent ON(node.rapkdet_lft BETWEEN sub_parent.rapkdet_lft AND sub_parent.rapkdet_rgt)
								JOIN (
									SELECT node.rapkdet_id, (COUNT(parent.rapkdet_id) - 1) AS depth
									FROM t_rapkdet AS node JOIN t_rapkdet AS parent ON(node.rapkdet_lft BETWEEN parent.rapkdet_lft AND parent.rapkdet_rgt)
									WHERE node.t_rapk_id = ".$master_id." AND node.rapkdet_id = ".$t_rapkdet_group."
									GROUP BY node.rapkdet_id
									ORDER BY node.rapkdet_lft
								)AS sub_tree ON(sub_parent.rapkdet_id = sub_tree.rapkdet_id)
								LEFT JOIN m_jobdet ON(m_jobdet.jobdet_id = node.m_jobdet_id)
								LEFT JOIN m_unit ON(m_unit.unit_id = node.rapkdet_unit)
							GROUP BY node.rapkdet_id
							HAVING depth = 1
							ORDER BY node.rapkdet_lft";
					
				}else {
					$query = "SELECT rapkdet_id, jobdet_id, jobdet_name, rapkdet_volume, rapkdet_price, rapkdet_publish, unit_id, unit_name
							FROM t_rapkdet JOIN m_jobdet ON(t_rapkdet.m_jobdet_id = m_jobdet.jobdet_id)
								LEFT JOIN m_unit ON(m_unit.unit_id = t_rapkdet.rapkdet_unit)
							WHERE t_rapk_id=".$master_id;
				}
				
			}elseif ($task=='byjob'){
				$query = "SELECT 0 AS rapkdet_id, jobdet_id, jobdet_name, 0 AS rapkdet_volume, 0 AS rapkdet_price, 0 AS rapkdet_publish, '' AS unit_id, '' AS unit_name
						FROM m_jobdet WHERE m_job_id=".$master_id;
			}
			
			
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
			$limit = $query." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);
				
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		function get_parentname_byrapk($master_id){
			$query = "SELECT rapkdet_id, rapkdet_uraian 
					FROM t_rapkdet 
					WHERE t_rapkdet.rapkdet_uraian IS NOT NULL AND t_rapkdet.t_rapk_id = ".$master_id;
			
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
			
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		function t_rapkdet_grouptemp_getAll($by){
			$query = "SELECT group_id, group_uraian, group_publish
					FROM t_rapkdet_grouptemp
					WHERE group_created_by = '".$by."'
					ORDER BY group_id DESC";
				
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
				
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		function get_rapktotal($by){
			$sql = "
				SELECT SUM(rapkdet_volume * rapkdet_price) as total FROM
				(SELECT rapkdet_volume,rapkdet_price, if (  publish = 1 , 1 , rapkdet_publish) as publish
				FROM 
					(SELECT t_rapkdet_groupdettemp.*  FROM `t_rapkdet_groupdettemp`
					JOIN
					t_rapkdet_grouptemp
					on t_rapkdet_groupdettemp.group_id = t_rapkdet_grouptemp.group_id
					WHERE t_rapkdet_grouptemp.group_created_by = '".$by."'
				) u
				LEFT JOIN (
				SELECT group_id, if ( sum(rapkdet_publish) > 0 , 0, 1) as publish FROM t_rapkdet_groupdettemp
				group by group_id ) t
				On u.group_id = t.group_id
				) k
				where publish = 1
			";
			$query = $this->db->query($sql);
		//	$row = $query->row('total');
			return $query->result();
		}
		
		function t_rapkdet_groupdettemp_getAll($group_id){
			$query = "SELECT groupdettemp_id, jobdet_id, jobdet_name, rapkdet_volume, unit_id, unit_name, rapkdet_price, rapkdet_publish
					FROM t_rapkdet_groupdettemp JOIN m_jobdet ON(m_jobdet.jobdet_id = t_rapkdet_groupdettemp.m_jobdet_id)
					LEFT JOIN m_unit ON(m_unit.unit_id = t_rapkdet_groupdettemp.rapkdet_unit)
					WHERE t_rapkdet_groupdettemp.group_id = ".$group_id;
		
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
		
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		function t_rapkdet_groupdettemp_getByGroup($group){
			$query = "SELECT jobdet_id, jobdet_name, rapkdet_volume, unit_id, unit_name, rapkdet_price, rapkdet_publish
					FROM t_rapkdet_groupdettemp JOIN m_jobdet ON(m_jobdet.jobdet_id = t_rapkdet_groupdettemp.m_jobdet_id)
					LEFT JOIN m_unit ON(m_unit.unit_id = t_rapkdet_groupdettemp.rapkdet_unit)
					WHERE t_rapkdet_groupdettemp.group_id = ".$group;
			
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
			
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for get list record
		function t_rapk_list($filter,$start,$end){
			$sql = "SELECT rapk_id, branch_id, branch_name, project_code, project_name, job_id,rapk_procurement, job_name, t_rapk.m_div_id, m_div.div_id, m_div.div_name
					FROM t_rapk 
					JOIN t_project ON(t_project.project_id = t_rapk.t_project_id)
					JOIN m_job ON(m_job.job_id = t_rapk.m_job_id) 
					LEFT JOIN m_branch ON(m_branch.branch_id = t_project.m_branch_id)
					LEFT JOIN m_div ON(m_div.div_id = t_rapk.m_div_id)";
			
			$sql_nbrows = $sql." GROUP BY t_rapk.rapk_id";
			
			// For simple search
			if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (project_code LIKE '%".addslashes($filter)."%' )";
			}

			$result = $this->db->query($sql);
			$result_nbrows = $this->db->query($sql_nbrows);
			$nbrows = $result_nbrows->num_rows();
			$limit = $sql." LIMIT ".$start.", ".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function t_rapk_create_sup($rapk_total,$procurement,$div_id ,$branch, $project, $job, $array_jobdet_id, $array_jobdet_name, $array_rapkdet_volume,
				$array_rapkdet_unit, $array_rapkdet_price){
			$data = array(
					"m_branch_id"=>$branch,
					"t_project_id"=>$project,
					"rapk_total"=>$rapk_total,
					"m_job_id"=>$job,
					"m_div_id"=>$div_id ,
					"rapk_procurement" => $procurement
			);
			$this->db->insert('t_rapk', $data);
			if($this->db->affected_rows()){
				$rapk_id = $this->db->insert_id();
				for($i = 0; $i < sizeof($array_jobdet_name); $i++){
					$jobdet_id = $array_jobdet_id[$i];
					$rapkdet_volume = $array_rapkdet_volume[$i];
					$rapkdet_unit = $array_rapkdet_unit[$i];
					$rapkdet_price = $array_rapkdet_price[$i];
			
					$data_detail = array(
							"t_rapk_id"=>$rapk_id,
							"m_jobdet_id"=>$jobdet_id,
							"rapkdet_volume"=>$rapkdet_volume,
							"rapkdet_unit"=>$rapkdet_unit,
							"rapkdet_price"=>$rapkdet_price
					);
					$this->db->insert('t_rapkdet', $data_detail);
				}
			
				return '1';
			}else{
				return '0';
			}
		}
		
		function t_rapk_create_sub($rapk_total,$procurement,$branch, $project, $job, $array_detailpekerjaan, $array_parentcheck,$div_id ){
			/*for ($i=0; $i<(sizeof($array_parentcheck)); $i++){
				$this->firephp->log($array_parentcheck[$i]);
			}
			return '1';*/
			
/* 			//get div_id by $job
			$sql = "SELECT div_id FROM m_div WHERE div_name = '".$div_id."'";
			$rs = $this->db->query($sql);
			$record = $rs->row();
			$m_div_id = $record->div_id; */
			
			$data = array(
					"m_branch_id"=>$branch,
					"t_project_id"=>$project,
					"m_job_id"=>$job,
					"rapk_total"=>$rapk_total,
					"m_div_id"=>$div_id,
					"rapk_procurement"=>$procurement
					);
			$this->db->insert('t_rapk', $data);
			if($this->db->affected_rows()){
				$rapk_id = $this->db->insert_id();
				
		/*		if($m_div_id == 1){ // edited by unu	
					//supplier
					$arr = $array_detailpekerjaan[0];
					foreach ($arr as $rows){
						foreach ($rows as $row){
							$data_detail = array(
									"t_rapk_id"=>$rapk_id,
									"m_jobdet_id"=>$row->jobdet_id,
									"rapkdet_volume"=>$row->rapkdet_volume,
									"rapkdet_unit"=>$row->unit_id,
									"rapkdet_price"=>$row->rapkdet_price
							);
							$this->db->insert('t_rapkdet', $data_detail);
						}
					} 
				}else{
					//sub-kontraktor
					$i = 0;
					foreach ($array_detailpekerjaan as $rows){
					*/ //
					$by = @$_SESSION[SESSION_USERID];
					$sql = "SELECT * FROM t_rapkdet_grouptemp WHERE group_created_by like '".$by."'";
					$query = $this->db->query($sql);
					foreach ( $query->result() as $rows ){ 
					
						$sql = "SELECT IFNULL((MAX(rapkdet_rgt)+1), 1) AS max_rgt
							FROM t_rapkdet";
						$rs = $this->db->query($sql);
						$myLeft = $rs->row()->max_rgt;
						
						//1. Add Parent === Uraian
			/*			$lock_tbl = "LOCK TABLE t_rapkdet WRITE";	// edited by unu	
						$this->db->query($lock_tbl);
						
						$sqlu = "UPDATE t_rapkdet SET rapkdet_rgt = rapkdet_rgt + 2 WHERE rapkdet_rgt > ".$myLeft;
						$this->db->query($sqlu);
						$sqlu = "UPDATE t_rapkdet SET rapkdet_lft = rapkdet_lft + 2 WHERE rapkdet_lft > ".$myLeft;
						$this->db->query($sqlu);
			*/ 			
						$data = array(
								"t_rapk_id"=>$rapk_id,
								"rapkdet_uraian"=>$rows->group_uraian, // tanyakan nanti key($rows)
								"rapkdet_publish"=>1,
								"rapkdet_lft"=>$myLeft,
								"rapkdet_rgt"=>$myLeft+1
						);
						$this->db->insert('t_rapkdet', $data);
						$create_affected = $this->db->affected_rows();
						$id = $this->db->insert_id();
							
			//			$unlock_tbl = "UNLOCK TABLES"; // edited by unu	
			//			$this->db->query($unlock_tbl);
						
						//2. Add Child of Parent
						$sql = "SELECT * FROM t_rapkdet_groupdettemp WHERE group_id = ".$rows->group_id;
						$query = $this->db->query($sql);
					
						foreach ($query->result() as $row){
							//$this->firephp->log($row[0]->jobdet_id);
							//$this->firephp->log($row);
					//		foreach ($row as $leaf){
								$sql = "SELECT rapkdet_lft FROM t_rapkdet WHERE rapkdet_id = ".$id;
								$rs = $this->db->query($sql);
								$myLeft = $rs->row()->rapkdet_lft;
								
					//			$lock_tbl = "LOCK TABLE t_rapkdet WRITE"; // edited by unu	
					//			$this->db->query($lock_tbl);
									
								$sqlu = "UPDATE t_rapkdet SET rapkdet_rgt = rapkdet_rgt + 2 WHERE rapkdet_rgt > ".$myLeft;
								$this->db->query($sqlu);
								$sqlu = "UPDATE t_rapkdet SET rapkdet_lft = rapkdet_lft + 2 WHERE rapkdet_lft > ".$myLeft;
								$this->db->query($sqlu);
									
								$data = array(
										"t_rapk_id"=>$rapk_id,
										"m_jobdet_id"=>$row->m_jobdet_id,
										"rapkdet_volume"=>$row->rapkdet_volume,
										"rapkdet_unit"=>$row->rapkdet_unit,
										"rapkdet_price"=>$row->rapkdet_price,
										"rapkdet_publish"=>$row->rapkdet_publish,
										"rapkdet_lft"=>$myLeft+1,
										"rapkdet_rgt"=>$myLeft+2
								);
								$this->db->insert('t_rapkdet', $data);
								$create_affected = $this->db->affected_rows();
									
					//			$unlock_tbl = "UNLOCK TABLES"; // edited by unu	
					//			$this->db->query($unlock_tbl);
					//		}
							
						}
						
					//	$i++;
					}
					
			//	}
				
				return '1';
			}else{
				return '0';
			}
		}
		
		function t_rapk_update_sub($rapk_total,$rapk_id){
		
			
				$data = array(
					"rapk_total"=>$rapk_total
					);

				$this->db->where('rapk_id',$rapk_id);
				$this->db->update('t_rapk',$data);
					
		
			// pertama-tama dihapus dulu
			$sql = "DELETE FROM t_rapkdet WHERE t_rapk_id = ".$rapk_id;
			$this->db->query($sql);
			// sekarang yang ada di temporary di masukan lagi
			
			$by = @$_SESSION[SESSION_USERID];
			$sql = "SELECT * FROM t_rapkdet_grouptemp WHERE group_created_by like '".$by."'";
			$query = $this->db->query($sql);
			foreach ( $query->result() as $rows ){ 
				$sql = "SELECT IFNULL((MAX(rapkdet_rgt)+1), 1) AS max_rgt
					FROM t_rapkdet";
				$rs = $this->db->query($sql);
				$myLeft = $rs->row()->max_rgt;
					
				$data = array(
					"t_rapk_id"=>$rapk_id,
					"rapkdet_id"=>$rows->rapkdet_id,
					"rapkdet_uraian"=>$rows->group_uraian, // tanyakan nanti key($rows)
					"rapkdet_publish"=>1,
					"rapkdet_lft"=>$myLeft,
					"rapkdet_rgt"=>$myLeft+1
				);
				$this->db->insert('t_rapkdet', $data);
				$create_affected = $this->db->affected_rows();
				$id = $this->db->insert_id();
				
				$sql = "SELECT * FROM t_rapkdet_groupdettemp WHERE group_id = ".$rows->group_id;
				$query = $this->db->query($sql);
					
				foreach ($query->result() as $row){
					$sql = "SELECT rapkdet_lft FROM t_rapkdet WHERE rapkdet_id = ".$id;
					$rs = $this->db->query($sql);
					$myLeft = $rs->row()->rapkdet_lft;
									
					$sqlu = "UPDATE t_rapkdet SET rapkdet_rgt = rapkdet_rgt + 2 WHERE rapkdet_rgt > ".$myLeft;
					$this->db->query($sqlu);
					$sqlu = "UPDATE t_rapkdet SET rapkdet_lft = rapkdet_lft + 2 WHERE rapkdet_lft > ".$myLeft;
					$this->db->query($sqlu);
									
					$data = array(
						"t_rapk_id"=>$rapk_id,
						"rapkdet_id"=>$row->rapkdet_id,
						"m_jobdet_id"=>$row->m_jobdet_id,
						"rapkdet_volume"=>$row->rapkdet_volume,
						"rapkdet_unit"=>$row->rapkdet_unit,
						"rapkdet_price"=>$row->rapkdet_price,
						"rapkdet_publish"=>$row->rapkdet_publish,
						"rapkdet_lft"=>$myLeft+1,
						"rapkdet_rgt"=>$myLeft+2
					);
					$this->db->insert('t_rapkdet', $data);
					$create_affected = $this->db->affected_rows();
				}
			}
			return 1;
		}

		//function for update record
		function t_rapk_update($rapk_total,$rapk_id, $branch, $project, $job, $array_rapkdet_id, $array_jobdet_id, $array_jobdet_name, $array_rapkdet_volume,
				$array_rapkdet_unit, $array_rapkdet_price){
				
				$data = array(
					"rapk_total"=>$rapk_total
					);

				$this->db->where('rapk_id',$rapk_id);
				$this->db->update('t_rapk',$data);
					
					
				$query = "DELETE FROM t_rapkdet WHERE t_rapk_id = ".$rapk_id;
				$this->db->query($query );
				
			for($i = 0; $i < sizeof($array_jobdet_id); $i++){
				$rapkdet_id = $array_rapkdet_id[$i];
				$jobdet_id = $array_jobdet_id[$i];
				$rapkdet_volume = $array_rapkdet_volume[$i];
				$rapkdet_unit = $array_rapkdet_unit[$i];
				$rapkdet_price = $array_rapkdet_price[$i];
				
			//	if($rapkdet_id == 0){
					//insert data
					$data_c = array(
							"rapkdet_id"=>$rapkdet_id,
							"t_rapk_id"=>$rapk_id,
							"m_jobdet_id"=>$jobdet_id,
							"rapkdet_volume"=>$rapkdet_volume,
							"rapkdet_unit"=>$rapkdet_unit,
							"rapkdet_price"=>$rapkdet_price
							);
					$this->db->insert('t_rapkdet', $data_c);
		/*		}else{
					$data_u = array(
							"rapkdet_volume"=>$rapkdet_volume,
							"rapkdet_unit"=>$rapkdet_unit,
							"rapkdet_price"=>$rapkdet_price
							);
					$this->db->where('rapkdet_id', $rapkdet_id);
					$this->db->update('t_rapkdet', $data_u);
				}
			*/		
			}
			
			return '1';
		}

		//fcuntion for delete record
		function t_rapk_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the t_rapks at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "SELECT t_komp.t_project_id FROM t_komp 
						LEFT JOIN t_rapk
						ON t_komp.m_branch_id = t_rapk.m_branch_id 
						AND t_komp.m_job_id = t_rapk.m_job_id
						AND t_komp.t_project_id  = t_rapk.t_project_id 
						where t_rapk.rapk_id = ".$pkid[0];
				$query = $this->db->query($sql);
				if ($query->num_rows() == 0 ){
					$sql = "DELETE FROM t_rapk WHERE rapk_id = ".$pkid[0];
					$this->db->query($sql);
					$sql = "DELETE FROM t_rapkdet WHERE t_rapk_id = ".$pkid[0];
					$this->db->query($sql);
					return '1';
				}else{
					return '3';
				}
			} else {
				$sql_check = "SELECT t_komp.t_project_id FROM t_komp 
							LEFT JOIN t_rapk
							ON t_komp.m_branch_id = t_rapk.m_branch_id 
							AND t_komp.m_job_id = t_rapk.m_job_id
							AND t_komp.t_project_id  = t_rapk.t_project_id 
							where  "; 
				$sql = "DELETE FROM t_rapk WHERE ";
				$sqldet = "DELETE FROM t_rapkdet WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "rapk_id = ".$pkid[$i];
					$sqldet = $sqldet . "t_rapk_id = ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
						$sqldet = $sqldet . " OR ";
					}
					
					$sql_check = $sql_check . "t_rapk.rapk_id = ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql_check = $sql_check . " OR ";
					}
				}
				$query = $this->db->query($sql_check);
				if ($query->num_rows() == 0 ){
					$this->db->query($sql);
					$this->db->query($sqldet);
					return '1';
				}else{
					return '3';
				}
			}
		}
		
		function t_rapkdet_detailtemp_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the t_rapks at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM t_rapkdet_groupdettemp WHERE groupdettemp_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM t_rapkdet_groupdettemp WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "groupdettemp_id = ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}
		function t_rapkdet_grouptemp_truncate($by){
			$sql = "SELECT * FROM t_rapkdet_grouptemp WHERE t_rapkdet_grouptemp.group_created_by = '".$by."' OR t_rapkdet_grouptemp.group_created_by is null ";
			$query = $this->db->query($sql);
			foreach($query->result() as $result){
				$sql_2 = "DELETE FROM t_rapkdet_groupdettemp WHERE group_id = ".$result->group_id ;
				$query_2 = $this->db->query($sql_2);
			}
			$sql = "DELETE 
					FROM t_rapkdet_grouptemp 
					WHERE t_rapkdet_grouptemp.group_created_by = '".$by."' OR t_rapkdet_grouptemp.group_created_by is null";

			$this->db->query($sql);
			
			$this->db->query("ALTER TABLE t_rapkdet_grouptemp AUTO_INCREMENT = 1");
			$this->db->query("ALTER TABLE t_rapkdet_groupdettemp AUTO_INCREMENT = 1");
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			} 
		}
		
		function t_rapkdet_grouptemp_create($group_uraian, $group_publish, $by){
			$data = array(
					"group_uraian"=>$group_uraian,
					"group_publish"=>1,
					"group_created_by"=>$by
			);
			$this->db->insert('t_rapkdet_grouptemp', $data);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		
		function t_rapkdet_grouptemp_update($group_id,$group_uraian, $group_publish, $by){
			$data = array(
					"group_uraian"=>$group_uraian,
					"group_publish"=>1,
					"group_created_by"=>$by
			);
			$this->db->where('group_id',$group_id);
			$this->db->update('t_rapkdet_grouptemp', $data);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		
		function t_rapkdet_grouptemp_delete($group_id){
			$sqld = "DELETE t_rapkdet_grouptemp, t_rapkdet_groupdettemp 
					FROM t_rapkdet_grouptemp LEFT JOIN t_rapkdet_groupdettemp ON(t_rapkdet_groupdettemp.group_id = t_rapkdet_grouptemp.group_id)
					WHERE t_rapkdet_grouptemp.group_id = ".$group_id;
			$this->db->query($sqld);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		
		function t_rapkdet_groupdettemp_create($group_id, $m_jobdet_id, $rapkdet_volume, $rapkdet_unit, $rapkdet_price, $rapkdet_publish){
			$data = array(
					"group_id"=>$group_id,
					"m_jobdet_id"=>$m_jobdet_id,
					"rapkdet_volume"=>$rapkdet_volume,
					"rapkdet_unit"=>$rapkdet_unit,
					"rapkdet_price"=>$rapkdet_price,
					"rapkdet_publish"=>$rapkdet_publish
			);
			$this->db->insert('t_rapkdet_groupdettemp', $data);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		
		
		function t_rapkdet_groupdettemp_update($groupdettemp_id,$group_id, $m_jobdet_id, $rapkdet_volume, $rapkdet_unit, $rapkdet_price, $rapkdet_publish){
			$data = array(
					"group_id"=>$group_id,
					"m_jobdet_id"=>$m_jobdet_id,
					"rapkdet_volume"=>$rapkdet_volume,
					"rapkdet_unit"=>$rapkdet_unit,
					"rapkdet_price"=>$rapkdet_price,
					"rapkdet_publish"=>$rapkdet_publish
			);
			$this->db->where('groupdettemp_id',$groupdettemp_id);
			$this->db->update('t_rapkdet_groupdettemp', $data);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		//function for advanced search record
		function t_rapk_search($rapk_id,
				 $rapk_flag,
				 $rapk_code,
				 $rapk_type,
				 $rapk_name,
				 $rapk_shortname,
				 $rapk_pm,
				 $rapk_semcm,
				 $rapk_value,
				 $rapk_start,
				 $rapk_p1,
				 $rapk_p2,
				 $rapk_created_date,
				 $rapk_created_by,
				 $rapk_updated_date,
				 $rapk_updated_by,
				 $rapk_updated_count,
				 $m_branch,$start,$end){
			//full query
			$sql = "SELECT rapk_id,
						   rapk_flag,
						   rapk_code,
						   rapk_type,
						   rapk_name,
						   rapk_shortname,
						   rapk_pm,
						   rapk_semcm,
						   rapk_value,
						   rapk_start,
						   rapk_p1,
						   rapk_p2,
						   rapk_created_date,
						   rapk_created_by,
						   rapk_updated_date,
						   rapk_updated_by,
						   rapk_updated_count,
						   m_branch_id
					FROM   t_rapk";

			if($rapk_id!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_id) LIKE '%".addslashes(strtolower($rapk_id))."%'";
			};
			if($rapk_flag!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_flag) LIKE '%".addslashes(strtolower($rapk_flag))."%'";
			};
			if($rapk_code!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_code) LIKE '%".addslashes(strtolower($rapk_code))."%'";
			};
			if($rapk_type!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_type) LIKE '%".addslashes(strtolower($rapk_type))."%'";
			};
			if($rapk_name!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_name) LIKE '%".addslashes(strtolower($rapk_name))."%'";
			};
			if($rapk_shortname!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_shortname) LIKE '%".addslashes(strtolower($rapk_shortname))."%'";
			};
			if($rapk_pm!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_pm) LIKE '%".addslashes(strtolower($rapk_pm))."%'";
			};
			if($rapk_semcm!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_semcm) LIKE '%".addslashes(strtolower($rapk_semcm))."%'";
			};
			if($rapk_value!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_value) LIKE '%".addslashes(strtolower($rapk_value))."%'";
			};
			if($rapk_start!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_start) LIKE '%".addslashes(strtolower($rapk_start))."%'";
			};
			if($rapk_p1!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_p1) LIKE '%".addslashes(strtolower($rapk_p1))."%'";
			};
			if($rapk_p2!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_p2) LIKE '%".addslashes(strtolower($rapk_p2))."%'";
			};
			if($rapk_created_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_created_date) LIKE '%".addslashes(strtolower($rapk_created_date))."%'";
			};
			if($rapk_created_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_created_by) LIKE '%".addslashes(strtolower($rapk_created_by))."%'";
			};
			if($rapk_updated_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_updated_date) LIKE '%".addslashes(strtolower($rapk_updated_date))."%'";
			};
			if($rapk_updated_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_updated_by) LIKE '%".addslashes(strtolower($rapk_updated_by))."%'";
			};
			if($rapk_updated_count!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(rapk_updated_count) LIKE '%".addslashes(strtolower($rapk_updated_count))."%'";
			};
			if($m_branch!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(m_branch_id) LIKE '%".addslashes(strtolower($m_branch))."%'";
			};

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function t_rapk_print($rapk_id,
				 $rapk_flag,
				 $rapk_code,
				 $rapk_type,
				 $rapk_name,
				 $rapk_shortname,
				 $rapk_pm,
				 $rapk_semcm,
				 $rapk_value,
				 $rapk_start,
				 $rapk_p1,
				 $rapk_p2,
				 $rapk_created_date,
				 $rapk_created_by,
				 $rapk_updated_date,
				 $rapk_updated_by,
				 $rapk_updated_count,
				 $m_branch,$option,$filter){
			//full query
			$sql = "SELECT rapk_id,
						   rapk_flag,
						   rapk_code,
						   rapk_type,
						   rapk_name,
						   rapk_shortname,
						   rapk_pm,
						   rapk_semcm,
						   rapk_value,
						   rapk_start,
						   rapk_p1,
						   rapk_p2,
						   rapk_created_date,
						   rapk_created_by,
						   rapk_updated_date,
						   rapk_updated_by,
						   rapk_updated_count,
						   m_branch_id
				    FROM   t_rapk";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(rapk_id) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_flag) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_code) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_type) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_name) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_shortname) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_pm) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_semcm) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_value) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_start) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_p1) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_p2) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_created_date) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_created_by) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_updated_date) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_updated_by) LIKE '%".addslashes(strtolower($filter))."%' OR lower(rapk_updated_count) LIKE '%".addslashes(strtolower($filter))."%' OR lower(m_branch_id) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				if($rapk_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_id) LIKE '%".addslashes(strtolower($rapk_id))."%'";
				};
				if($rapk_flag!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_flag) LIKE '%".addslashes(strtolower($rapk_flag))."%'";
				};
				if($rapk_code!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_code) LIKE '%".addslashes(strtolower($rapk_code))."%'";
				};
				if($rapk_type!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_type) LIKE '%".addslashes(strtolower($rapk_type))."%'";
				};
				if($rapk_name!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_name) LIKE '%".addslashes(strtolower($rapk_name))."%'";
				};
				if($rapk_shortname!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_shortname) LIKE '%".addslashes(strtolower($rapk_shortname))."%'";
				};
				if($rapk_pm!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_pm) LIKE '%".addslashes(strtolower($rapk_pm))."%'";
				};
				if($rapk_semcm!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_semcm) LIKE '%".addslashes(strtolower($rapk_semcm))."%'";
				};
				if($rapk_value!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_value) LIKE '%".addslashes(strtolower($rapk_value))."%'";
				};
				if($rapk_start!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_start) LIKE '%".addslashes(strtolower($rapk_start))."%'";
				};
				if($rapk_p1!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_p1) LIKE '%".addslashes(strtolower($rapk_p1))."%'";
				};
				if($rapk_p2!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_p2) LIKE '%".addslashes(strtolower($rapk_p2))."%'";
				};
				if($rapk_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_created_date) LIKE '%".addslashes(strtolower($rapk_created_date))."%'";
				};
				if($rapk_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_created_by) LIKE '%".addslashes(strtolower($rapk_created_by))."%'";
				};
				if($rapk_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_updated_date) LIKE '%".addslashes(strtolower($rapk_updated_date))."%'";
				};
				if($rapk_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_updated_by) LIKE '%".addslashes(strtolower($rapk_updated_by))."%'";
				};
				if($rapk_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_updated_count) LIKE '%".addslashes(strtolower($rapk_updated_count))."%'";
				};
				if($m_branch!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_branch_id) LIKE '%".addslashes(strtolower($m_branch))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function t_rapk_export_excel($rapk_id ,$rapk_flag ,$rapk_code ,$rapk_type ,$rapk_name ,$rapk_shortname ,$rapk_pm ,$rapk_semcm ,$rapk_value ,$rapk_start ,$rapk_p1 ,$rapk_p2 ,$rapk_created_date ,$rapk_created_by ,$rapk_updated_date ,$rapk_updated_by ,$rapk_updated_count ,$m_branch ,$option,$filter){
			//full query
			$sql = "SELECT rapk_id,
						   rapk_flag,
						   rapk_code,
						   rapk_type,
						   rapk_name,
						   rapk_shortname,
						   rapk_pm,
						   rapk_semcm,
						   rapk_value,
						   rapk_start,
						   rapk_p1,
						   rapk_p2,
						   rapk_created_date,
						   rapk_created_by,
						   rapk_updated_date,
						   rapk_updated_by,
						   rapk_updated_count,
						   m_branch_id				    FROM   t_rapk";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= "(lower(rapk_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_flag) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_code) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_type) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_name) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_shortname) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_pm) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_semcm) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_value) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_start) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_p1) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_p2) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_created_date) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_created_by) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_updated_date) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_updated_by) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(rapk_updated_count) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(m_branch_id) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				if($rapk_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_id) LIKE '%".addslashes(strtolower($rapk_id))."%'";
				};
				if($rapk_flag!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_flag) LIKE '%".addslashes(strtolower($rapk_flag))."%'";
				};
				if($rapk_code!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_code) LIKE '%".addslashes(strtolower($rapk_code))."%'";
				};
				if($rapk_type!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_type) LIKE '%".addslashes(strtolower($rapk_type))."%'";
				};
				if($rapk_name!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_name) LIKE '%".addslashes(strtolower($rapk_name))."%'";
				};
				if($rapk_shortname!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_shortname) LIKE '%".addslashes(strtolower($rapk_shortname))."%'";
				};
				if($rapk_pm!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_pm) LIKE '%".addslashes(strtolower($rapk_pm))."%'";
				};
				if($rapk_semcm!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_semcm) LIKE '%".addslashes(strtolower($rapk_semcm))."%'";
				};
				if($rapk_value!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_value) LIKE '%".addslashes(strtolower($rapk_value))."%'";
				};
				if($rapk_start!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_start) LIKE '%".addslashes(strtolower($rapk_start))."%'";
				};
				if($rapk_p1!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_p1) LIKE '%".addslashes(strtolower($rapk_p1))."%'";
				};
				if($rapk_p2!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_p2) LIKE '%".addslashes(strtolower($rapk_p2))."%'";
				};
				if($rapk_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_created_date) LIKE '%".addslashes(strtolower($rapk_created_date))."%'";
				};
				if($rapk_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_created_by) LIKE '%".addslashes(strtolower($rapk_created_by))."%'";
				};
				if($rapk_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_updated_date) LIKE '%".addslashes(strtolower($rapk_updated_date))."%'";
				};
				if($rapk_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_updated_by) LIKE '%".addslashes(strtolower($rapk_updated_by))."%'";
				};
				if($rapk_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(rapk_updated_count) LIKE '%".addslashes(strtolower($rapk_updated_count))."%'";
				};
				if($m_branch!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_branch_id) LIKE '%".addslashes(strtolower($m_branch))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $sql;
		}

}
?>